/*  JSCOUR - Java Scour search and download interface
 *  Copyright (C) 2000  jscour@priest.com
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package edu.hit.pact.pgse.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


/**
 * @author jscour@priest.com
 */
public class Md5Sum {

	/**
	 * @param str the String from which to calculate the sum
	 * @return the MD5 checksum
	 */
	public static byte[] getMd5sum(String str) throws IllegalStateException {
		try {
			return getMd5sum(str.getBytes("UTF-8"));
		} catch (Exception e) {
			throw new IllegalStateException(e.getMessage());
		}
	}

	public static byte[] getMd5sum(byte[] input) throws IllegalStateException {
		return getMd5sum(input, -1);
	}

	public static byte[] getMd5sum(byte[] input, int limit)
			throws IllegalStateException {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");

			md.reset();

			byte[] digest;

			if (limit == -1) {
				digest = md.digest(input);
			} else {
				md.update(input, 0, limit > input.length ? input.length
								: limit);
				digest = md.digest();
			}

			return digest;
		} catch (NoSuchAlgorithmException e) {
			throw new IllegalStateException(e.getMessage());
		}
	}

	public static long getMd5LongSum(String str) {
		byte[] digest = getMd5sum(str);

		return getLong(digest);
	}

	private static long getLong(byte[] buf) {
		long r = 0;
    	
		for (int i = 0; i < buf.length; i++) {
			r <<= 4;
			r |= (buf[i] & 0xFF);
		}

		return r;
	}

    public static String changeTo18String(String url){
		
		long urlId1 = getMd5LongSum(url);
		String temp = Long.toString(urlId1);
		if (temp.length() > 18){
			temp = temp.substring(temp.length() - 18, temp.length());
		}
		else if(temp.length() < 18){
			String tempStr ;
			do{
				tempStr = String.valueOf((long)(Math.random()*1000*1000*1000)) + String.valueOf((long)(Math.random()*1000*1000*1000));
			}while(tempStr.length() < 18);
			
			temp =  tempStr.substring(1,18 - temp.length() + 2) + temp.substring(1,temp.length());//除去temp或者tempStr前面的负号，并且将temp补齐到18个字符长
		}
//		System.out.println(temp.length() + "tts\n" + urlId1);
		return temp;
		
	}
	
	/*
	 * Test Driver
	 */
	public static void main(String[] args) {
		String url = "a";
		String url2 = "http://172.16.15.240:8080/webmds/webmds?info=indexinfo&xsl=servicegroupxsl";

		long urlId1 = getMd5LongSum(url);
//		long urlId2 = getMd5LongSum(url2);

		System.out.println(urlId1);
//		System.out.println(urlId2);
//		byte a = 127;
//		
//		System.out.println((int)a);
	}
}
